import { LogHelper } from "../../utils/logHelper";

export class OuterOreReserveTask implements ICreepTask{
  run(creep: Creep): void {
    if(creep.memory.targetRoom){
      const room = Game.rooms[creep.memory.targetRoom]

      if(!room){
        creep.go(new RoomPosition(25,25,creep.memory.targetRoom));
      }else{
        const controller = room.controller;
        if(controller){
          if(creep.reserveController(controller) == ERR_NOT_IN_RANGE) {
            let result = creep.go(controller);
          }
        }
      }

      // 如果为外矿的采集 自动申请
      if(creep.ticksToLive && creep.ticksToLive <= 30){
        if(creep.memory.source && !creep.memory.spawnApplyFor){
          const room = Game.rooms[creep.memory.bornRoom];
          const role = 'outerOre_reserve';
          const body = room.calculateCreepBody({
            [MOVE]:0.5,
            [CLAIM]:0.5
          });
          room.spawnQueueAdd(role,body,{ role:role,bornRoom:creep.memory.bornRoom ,targetRoom:creep.memory.targetRoom });
          creep.memory.spawnApplyFor = true;
          LogHelper.log(`外矿占领 Creep [${creep.name}] 申请孵化 ${role}`);
        }
      }
    }
  }
}